package uilayer;

import java.util.InputMismatchException;
import java.util.Scanner;
import modellayer.*;
import controllayer.*;

/**
 * Textbased UI which allows the user to input information about orders.
 * 
 *@author Benjamin Ørskov
 *@author Lasse Lambertsen
 *@author Ole Kirk
 *@author Søren Søgaard
 */
public class OrderUI extends MenuInputs {

	// instance variables - object by the type oCtrl
	/** The order ctrl. */
	private OrderController oCtrl;

	/** The instance. */
	private static OrderUI instance;

	/**
	 * Constructor for OrderUI.
	 */
	private OrderUI() {
		// Initialize oCtrl
		oCtrl = new OrderController();
	}

	/**
	 * Gets the single instance of OrderUI.
	 *
	 * @return single instance of OrderUI
	 */
	public static OrderUI getInstance() {
		if (instance == null) {
			instance = new OrderUI();
		}
		return instance;
	}

	/**
	 * Order menu.
	 */
	public void orderMenu() {
		boolean exit = false;
		try {
			while (!exit) { // that is: while exit is false
				int choice = writeOrderMenu();
				if (choice == 1) { // Create order
					Order o = oCtrl.createOrder();
					createOrderMenu(o);
				} else if (choice == 2) { // find order
					try {
						oCtrl.findOrder(inputOrderID());
					} catch (NullPointerException e) {
						System.out.println(e.getMessage());
					}
				} else if (choice == 3) {
					try {
						oCtrl.removeOrder(inputOrderID());
					} catch (NullPointerException e) {
						System.out.println(e.getMessage());
					}
				} else {
					exit = true;
				}// end else
			}// end while
		}// end try
		catch (InputMismatchException e) {
			// do something clever with the exception
			System.out.println("Du skal indtaste et tal!");
			orderMenu();
		}
	}

	/**
	 * Display order menu.
	 *
	 * @return the int
	 */
	private int writeOrderMenu() { // makes an object keyboard to read input
		// from the screen
		Scanner keyboard = new Scanner(System.in);
		System.out.println("*** Ordre Menu ***");
		System.out.println(" (1) Opret ordre");
		System.out.println(" (2) Find ordre");
		System.out.println(" (3) Slet ordre");
		System.out.println(" (4) Tilbage");
		System.out.print("\n Vælg: ");

		int choice = keyboard.nextInt();
		return choice;
	}

	/**
	 * Creates the order menu.
	 *
	 * @param o
	 *            the o
	 */
	private void createOrderMenu(Order o) {
		boolean exit = false;
		try {
			while (!exit) { // that is: while exit is false
				int choice = writeCreateOrderMenu();
				if (choice == 1) { // Add customer
					chooseCustomerMenu(o);
				} else if (choice == 2) { // Add salesman
					try {
						Salesman tempS = (Salesman) oCtrl
								.findSalesman(inputSalesmanID());
						o.setSalesman(tempS);
					} catch (UpdateIncompleteException uie) {
						System.out.println(uie.getMessage());
					}
				} else if (choice == 3) {
					// Add item
					try {
						Item tempI = oCtrl.findItem(inputItemID());
						try {
							oCtrl.addItem(tempI, inputAmount(), o);
						} catch (NotEnoughItemsException neie) {
							System.out.println(neie.getMessage());
						}
					} catch (NullPointerException npe) {
						System.out.println(npe.getMessage());
					}
				} else if (choice == 4) {
					// Remove item
					try {
						Item tempI = oCtrl.findItem(inputItemID());
						oCtrl.removeItem(tempI.getItemID(), o);
					} catch (NullPointerException npe) {
						System.out.println(npe.getMessage());
					}
				} else if (choice == 5) {
					// End order
					try {
						System.out.println(o);
						// Update storage amount
						oCtrl.updateAmounts(o);
						// Update salesman revenue
						o.getSalesman().setRevenue(
								o.getSalesman().getRevenue() + o.getPrice());
					} catch (NullPointerException npe) {
						System.out.println("Ordre findes ikke!");
					}
				} else {
					if (o.getPrice() == 0.0) {
						oCtrl.removeOrder(o.getOrderID());
						oCtrl.setOrderID();
					}
					exit = true;
				}// end else
			}// end while
		}// end try
		catch (InputMismatchException e) {
			// do something clever with the exception
			System.out.println("Du skal indtaste et tal!");
			createOrderMenu(o);
		}
	}

	/**
	 * Write create order menu.
	 *
	 * @return the int
	 */
	private int writeCreateOrderMenu() { // makes an object keyboard to read
		// input from the screen
		Scanner keyboard = new Scanner(System.in);
		System.out.println("*** Opret ordre menu ***");
		System.out.println(" (1) Tilføj/rediger kunde");
		System.out.println(" (2) Tilføj/rediger s�lger");
		System.out.println(" (3) Tilføj vare");
		System.out.println(" (4) Fjern vare");
		System.out.println(" (5) Afslut ordre");
		System.out.println(" (6) Tilbage");
		System.out.print("\n Vælg: ");

		int choice = keyboard.nextInt();
		return choice;
	}

	/**
	 * Write choose customer menu.
	 *
	 * @return the int
	 */
	private int writeChooseCustomerMenu() { // makes an object keyboard to read
		// input from the screen
		Scanner keyboard = new Scanner(System.in);
		System.out.println("*** Vælg kunde menu ***");
		System.out.println(" (1) Tilføj erhvervskunde");
		System.out.println(" (2) Tilføj privatkunde");
		System.out.println(" (3) Tilbage");
		System.out.print("\n Vælg: ");

		int choice = keyboard.nextInt();
		return choice;
	}

	/**
	 * Choose customer menu.
	 *
	 * @param o
	 *            the o
	 */
	private void chooseCustomerMenu(Order o) {
		boolean exit = false;
		try {
			while (!exit) { // that is: while exit is false
				int choice = writeChooseCustomerMenu();
				if (choice == 1) { // Add BusinessCustomer
					try {
						BusinessCustomer tempBC = (BusinessCustomer) oCtrl
								.findBusinessCustomer(inputCvr());
						o.setCustomer(tempBC);
					} catch (UpdateIncompleteException uie) {
						System.out.println(uie.getMessage());
					}
				} else if (choice == 2) { // Add customer
					try {
						Customer tempC = (Customer) oCtrl
								.findCustomer(inputPhone());
						o.setCustomer(tempC);
					} catch (UpdateIncompleteException uie) {
						System.out.println(uie.getMessage());
					}
				} else {
					exit = true;
				}// end else
			}// end while
		}// end try
		catch (InputMismatchException e) {
			// do something clever with the exception
			System.out.println("Du skal indtaste et tal!");
			createOrderMenu(o);
		}
	}
}